//
//  CTVClipMenuItemLayer.m
//  Channltv
//
//  Created by Bruno Keymolen on 11/09/07.
//  Copyright 2007 channl.tv. All rights reserved.
//

#import "CTVClipMenuItemLayer.h"
#import "CTVCache.h"
#import "AppKit/NSColor.h"
#import "CTVUtil.h"

@implementation CTVClipMenuItemLayer

+(id)clipMenuItemWithScene:(BRRenderScene*)scene andThumbTexture:(BRBitmapTexture*)thumbTexture andRowHeight:(float)rowHeight andTitle:(NSString*)title andRate:(float)rate{
//+(id)clipMenuItemWithScene:(BRRenderScene*)scene andClip:(CTVClip*)clip andRowHeight:(float)rowHeight{
	CTVClipMenuItemLayer* _instance = [[self alloc] initWithScene:scene andThumbTexture:thumbTexture andRowHeight:rowHeight andTitle:title  andRate:rate];
	
	[_instance autorelease];
	return _instance;
}


-(id)initWithScene:(BRRenderScene*)scene andThumbTexture:(BRBitmapTexture*)thumbTexture andRowHeight:(float)rowHeight andTitle:(NSString*)title andRate:(float)rate{
//- (id)initWithScene:(BRRenderScene *)scene andClip:(CTVClip*)clip andRowHeight:(float)rowHeight{
	self = [super initWithScene:scene];



	[thumbTexture retain];
	m_thumbTexture = thumbTexture;
	
	m_rowHeight = rowHeight;	
			
	m_rate = rate;	
	m_watchPoint = -1.0f;
	
	//Background (the one that defines the height for the lozenge)
	m_backgroundLayer = [[BRQuadLayer alloc] initWithScene:[self scene]];
	[m_backgroundLayer setRedColor:0.00f greenColor:0.00f blueColor:0.00f];


	//Gray Layer (used to show the clip in a gray (or colored) rectangle)
	m_grayLayer = [[BRQuadLayer alloc] initWithScene:[self scene]];
	[m_grayLayer setRedColor:0.00f greenColor:0.00f blueColor:0.00f];

	//Title
	m_titleLayer = [BRTextMenuItemLayer menuItemWithScene:[self scene]];
	[m_titleLayer setTitle:title  withAttributes:[[BRThemeInfo sharedTheme] comboMenuItemTitleTextAttributes /*twoLineMenuItemSubtitleTextAttributes*/]];
	[m_titleLayer retain];
	
	//Thumb Left
	m_thumbLeftLayer = [[BRImageLayer alloc] init];	
	[m_thumbLeftLayer setTexture:m_thumbTexture];

	//Rating
	m_rateLayer = [[BRImageLayer alloc] init];	
	[m_rateLayer setTexture:[CTVCache getRateTextureForScene:scene andRate:m_rate]];
	
	//OnDisk
	m_onDiskLayer = [[BRImageLayer alloc] init];	
	[m_onDiskLayer setTexture:[CTVCache getOnDiskTextureForScene:scene]];

	
	//Stream
	m_streamLayer = [[BRImageLayer alloc] init];	
	[m_streamLayer setTexture:[CTVCache getStreamTextureForScene:scene]];
	
	//WatchPoint img
	m_watchPointLayer = [[BRImageLayer alloc] init];	
	[m_watchPointLayer setTexture:[CTVCache getWatchPointTextureForScene:scene]];

	
	//WatchPoint offset text (minutes)
	m_offsetTextLayer = [[BRTextLayer alloc] initWithScene:[self scene]];

	
	//Add all to background
	[self addSublayer:m_backgroundLayer];
 	
	[m_backgroundLayer addSublayer:m_grayLayer];
	[m_backgroundLayer addSublayer:m_titleLayer];
	[m_backgroundLayer addSublayer:m_thumbLeftLayer];
	[m_backgroundLayer addSublayer:m_rateLayer];
	[m_backgroundLayer addSublayer:m_onDiskLayer];
	[m_backgroundLayer addSublayer:m_streamLayer];
	[m_backgroundLayer addSublayer:m_watchPointLayer];
	[m_backgroundLayer addSublayer:m_offsetTextLayer];
	
	
	return self;
}


-(void)dealloc{	
	NSLog(@"CTVClipMenuItemLayer - dealloc");

	[m_backgroundLayer release];
	[m_titleLayer release];
	[m_onDiskLayer release];
	[m_streamLayer release];
	[m_grayLayer release];
	[m_thumbLeftLayer release];
	[m_rateLayer release];
	[m_thumbTexture release];
	[m_watchPointLayer release];
	[m_offsetTextLayer release];
	
	[super dealloc];
}


-(BRTextMenuItemLayer*)textItem{
	return m_titleLayer;
}


-(void)setRowHeight:(float)rowHeight{
	m_rowHeight = rowHeight;
}


-(BRBitmapTexture*)thumbTexture{
	return m_thumbTexture;
}


-(NSRect)frame{
	return [super frame];
}


/* 
 * Interface: BRMenuItemLayer
 */
- (void)highlight{
	[m_titleLayer highlight];
}

- (void)unHighlight{
	[m_titleLayer unHighlight];
}

- (void)scrollItemIfNecessary{
	[m_titleLayer scrollItemIfNecessary];
}


- (void)stopScrollingItem{
	[m_titleLayer stopScrollingItem];
}

- (float)defaultRowHeight{
	return m_rowHeight;
}

- (BOOL)canHighlight{
	return TRUE;
}

- (BOOL)usesFullSelectionLozengeFrame{
	return TRUE;
}



- (void)renderLayer{
	[self _layOut];
	[super renderLayer];
}



- (void)setFrame:(NSRect)frame{
	[super setFrame:frame];
}

-(void)setWatchPoint:(float)min{
	m_watchPoint = min;
}

-(float)watchPoint{
	return m_watchPoint;
}


/*
 * Private
 */

-(void)_layOut{

	NSRect frame = [self frame];

	frame.origin.x=0;
	frame.origin.y = 0;

	[m_backgroundLayer setFrame:frame];

	NSRect grayFrame = frame;
	grayFrame.size.height = frame.size.height * 0.85f; //slightly bigger than the thumb
	grayFrame.origin.y = (frame.size.height - grayFrame.size.height) * 0.5f; 
	[m_grayLayer setFrame:grayFrame];
	

	//Width & Height
	frame.size.height = frame.size.height * 0.80f; //We use 95% becuse there need to be space in between menu items
	frame.origin.y = ([m_backgroundLayer frame].size.height - frame.size.height) * 0.5f; 
	
	//Thumbnail - Left
	NSRect thumbLeftFrame;
	thumbLeftFrame.origin.x  = 0.0f;
	thumbLeftFrame.origin.y  = frame.origin.y;
	thumbLeftFrame.size.width = frame.size.width * 0.24f;
	thumbLeftFrame.size.height = frame.size.height;
	[m_thumbLeftLayer setFrame:thumbLeftFrame];

	//Text Top
	NSRect textTopFrame;
	textTopFrame.origin.x  = frame.size.width * 0.25f;
	textTopFrame.origin.y  = frame.origin.y + (frame.size.height * 0.60f);
	textTopFrame.size.width = frame.size.width * 0.75f;
	textTopFrame.size.height = frame.size.height * 0.40f;
	[m_titleLayer setFrame:textTopFrame];



	
	//Rating Icon
	NSRect rateFrame;
	rateFrame.origin.x  = frame.size.width * 0.25f;
	rateFrame.origin.y  = frame.origin.y;
	rateFrame.size.width = frame.size.width * 0.30f;
	rateFrame.size.height = frame.size.height * 0.35f;
	[m_rateLayer setFrame:rateFrame];
	
	//Data on disk icon
	if(m_clipOnDisk){
		[m_onDiskLayer setHidden:FALSE];
		NSRect onDiskFrame;
		onDiskFrame.origin.x  = frame.size.width * 0.80f;
		onDiskFrame.origin.y  = frame.origin.y;
		onDiskFrame.size.width = frame.size.width * 0.20f;
		onDiskFrame.size.height = frame.size.height * 0.45f;
		[m_onDiskLayer setFrame:onDiskFrame];
	}else{
		[m_onDiskLayer setHidden:TRUE];
	}
	
	
	//Stream  icon
	if(m_clipStream){
		[m_streamLayer setHidden:FALSE];
		NSRect streamFrame;
		if([m_onDiskLayer hidden]){
			streamFrame.origin.x  = frame.size.width * 0.80f;		
		}else{
			streamFrame.origin.x  = frame.size.width * 0.58f;
		}
		streamFrame.origin.y  = frame.origin.y;
		streamFrame.size.width = frame.size.width * 0.20f;
		streamFrame.size.height = frame.size.height * 0.45f;
		[m_streamLayer setFrame:streamFrame];
	}else{
		[m_streamLayer setHidden:TRUE];
	}
	

	//Watchpoint icon
	if(m_watchPoint > -1){
		[m_watchPointLayer setHidden:FALSE];
		[m_offsetTextLayer setHidden:FALSE];
		NSRect watchPointFrame;
		watchPointFrame.origin.x  = frame.size.width * 0.25f;
		watchPointFrame.origin.y  = frame.size.height * 0.50f;
		watchPointFrame.size.width = frame.size.height * 0.30f;
		watchPointFrame.size.height = frame.size.height * 0.30f;
		[m_watchPointLayer setFrame:watchPointFrame];
		
		NSRect offsetTextFrame;
		offsetTextFrame.origin.x  = (frame.size.width * 0.25f) + (frame.size.height * 0.33f) ;
		offsetTextFrame.origin.y  = frame.size.height * 0.50f;
		offsetTextFrame.size.width = frame.size.height * 0.30f;
		offsetTextFrame.size.height = frame.size.height * 0.30f;
		[m_offsetTextLayer setFrame:offsetTextFrame];
		NSString* min = [CTVUtil getTimeRepresentationString:(long)m_watchPoint fullDetail:TRUE fullNames:FALSE];
		
	//	NSString* min = [NSString stringWithFormat:@"%d sec", (long)m_watchPoint];
		NSMutableAttributedString* as = [[NSMutableAttributedString alloc] initWithString:min attributes: [[BRThemeInfo sharedTheme] twoLineMenuItemSubtitleTextAttributes] ];
		[m_offsetTextLayer setAttributedString:as];
//m_offsetTextLayer
	}else{
		[m_watchPointLayer setHidden:TRUE];
		[m_offsetTextLayer setHidden:TRUE];
	}	
	
} 


-(BOOL)clipOnDisk{
	return m_clipOnDisk;
}

-(BOOL)clipStream{
	return m_clipStream;
}

-(void)setClipOnDisk:(BOOL)flag{
	m_clipOnDisk = flag;
	[self renderLayer];
}

-(void)setClipStream:(BOOL)flag{
	m_clipStream = flag;
	[self renderLayer];
}

-(float)rate{
	return m_rate;
}

-(void)setRate:(float)rate{
	m_rate = rate;
	[m_rateLayer setTexture:[CTVCache getRateTextureForScene:[self scene] andRate:rate]];
	[self renderLayer];	
}



//NSObject
- (unsigned)retainCount{
//	NSLog(@"CTVClipMenuItemLayer - retainCount : %@", [m_titleLayer title] );
	unsigned u = [super retainCount];
	return u;
}

- (id)retain{
	return [super retain];
}

- (id)autorelease{
	return [super autorelease];
}

- (oneway void)release{
	[super release];
}



/* 
 * Design flaw in the Apple TV Backrow code
 */
- (NSRect)grayFrame{ 
	NSRect r = [m_backgroundLayer frame]; 
	if(r.size.height <= 0){
		r.size.height = m_rowHeight;
	}
	return r;
}

- (BOOL)isKindOfClass:(Class)cl{
	if(cl == [BRGrayMenuItemLayer class]){
		return TRUE;
	}
	return FALSE;
}



@end
